import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:ichinesereader/constants.dart';
import 'package:ichinesereader/controller/authentication.dart';
import 'package:ichinesereader/controller/books.dart';
import 'package:ichinesereader/models/book/BookSearchDto.dart';
import 'package:ichinesereader/widgets/buttons/localLanguageDropdown.dart';
import 'package:pointer_interceptor/pointer_interceptor.dart';

class CustomAppBarWithSearch extends StatefulWidget
    implements PreferredSizeWidget {
  CustomAppBarWithSearch(
      {Key? key, required this.title, required this.readingType})
      : preferredSize = Size.fromHeight(48),
        super(key: key);
  final String title;
  final NanhaiBookReadingTypes readingType;

  @override
  _CustomAppBarWithSearchState createState() =>
      new _CustomAppBarWithSearchState();

  @override
  final Size preferredSize;
}

class _CustomAppBarWithSearchState extends State<CustomAppBarWithSearch> {
  TextEditingController searchTextController = new TextEditingController();
  final BooksController _booksController = Get.find();
  final LoginController _loginController = Get.find();
  bool isSearch = false;

  @override
  Widget build(BuildContext context) {
    final signOutBtn = IconButton(
      icon: const Icon(Icons.logout_rounded),
      color: Get.theme.appBarTheme.foregroundColor,
      onPressed: () {
        _booksController.bookFolders.clear();
        _loginController.logOut();
        Future.delayed(Duration(milliseconds: 500), () {
          Get.offAllNamed('/login');
        });
      },
    );
    final title = Center(
      child: Wrap(
        alignment: WrapAlignment.center,
        children: <Widget>[
          isSearch
              ? TextFormField(
                  controller: searchTextController,
                  onFieldSubmitted: (query) {
                    if (query.trim() != '') {
                      BookSearchDto dto = new BookSearchDto();
                      dto.query = query;
                      _booksController.searchedBookItems.clear();
                      _booksController.fetchSearchedBookItems(0, 0, dto, '');
                    }
                  },
                  decoration: InputDecoration(
                      prefixIcon: Icon(Icons.search),
                      suffixIcon: IconButton(
                        icon: Icon(Icons.clear),
                        onPressed: () {
                          searchTextController.text = '';
                        },
                      ),
                      hintText: 'Search ...'.tr,
                      border: InputBorder.none),
                )
              : Text(
                  widget.title,
                  style: Get.theme.appBarTheme.titleTextStyle
                      ?.copyWith(fontFamily: "Montserrat"),
                  overflow: TextOverflow.ellipsis,
                ),
        ],
      ),
    );

    return AppBar(
      actions: <Widget>[
        Container(
          padding: const EdgeInsets.all(8.0),
          child: PointerInterceptor(child: LocalLanguageDropDown()),
        ),
        SizedBox(
          width: 8,
        ),
        PointerInterceptor(child: signOutBtn),
      ],
      title: title,
      elevation: 0,
    );
  }
}
